#ifndef  _FLASH_CIPHER_H_
#define  _FLASH_CIPHER_H_

enum VER_TYPE
{
    BL_VER = 0,
    SW_VER = 1    
};

/*
The number of the key in key chunk
---address 0--------
chunk header
---address 0x80-----
security key 1
---address 0x80+0x40-----
security key 2
---address 0x80+0x40*2-----
security key 3
---address 0x80+0x40*3-----
*/
enum FLASH_KEY_TYPE
{
    BL_FLASH_KEY =    0xFF,
    FIRST_FLASH_KEY = 0xFE,
    SECOND_FLASH_KEY = 0xFD,
    THIRD_FLASH_KEY = 0xFC,
    FORTH_FLASH_KEY = 0xFB
};

enum FLASH_DATA_TYPE
{
    DATA_FROM_SRAM = 0,
    DATA_FROM_BLOCK_ADDR = 1,
    DATA_FROM_BLOCK_ID = 2
};

typedef struct SRAM_UKEY
{
    UINT8 *uk;
    UINT32 len;
    UINT32 resv;
}SRAM_UKEY;

typedef struct CE_UKEY
{
    UINT8 pos;
    UINT32 resv;
}CE_UKEY;

typedef struct UKEY_INFO
{
    struct SRAM_UKEY *sram_ukey;
	struct CE_UKEY *ce_ukey;
    UINT32 ukey_type;
	UINT32 resv[3];
}UKEY_INFO;

typedef struct CRYPT_DATA_INFO
{
    UINT8 *buf;
    UINT32 blockid;
    UINT32 addr;
	UINT32 len;
    enum FLASH_DATA_TYPE data_type;
	UINT32 resv[2];
}CRYPT_DATA_INFO;

#define FLASH_CHUCK_ID_MASK   0xFFFF0000
#define FLASH_KEY_ID_MASK     0xFF

#define OTP_VER_MAX_LEN        16
#define OTP_ADDESS_BL_VER      0x74
#define OTP_ADDESS_SW_VER      0x78
#define MAX_VERSION_NUM        128

#define ALIASIX_BL_UK_LEN       			  16
#define ALIASIX_SW_UK_LEN       			  16

#define BL_KEY_CIPHER_MAGIC_NUMBER            0xadeafbee

void flash_cipher_buf_init(UINT8 *tmp_buf_addr, UINT32 tmp_buf_size);
INT32 encrypt_universal_key(UINT32 key_id);
INT32 decrypt_universal_key(UINT8 *pos, UINT32 key_chunk_id);
INT32 aes_cbc_decrypt_ram_chunk(UINT8 key_pos, UINT8 *out, UINT8 *data, UINT32 data_len);
INT32 aes_cbc_decrypt_flash_data(UINT8 key_pos, UINT32 block_addr, UINT32 block_len, UINT8 *out);
INT32 aes_cbc_decrypt_chunk(UINT8 key_pos, UINT32 blockid, UINT8 *out, UINT32 *len);
//for STD VMX boot and upgrade
INT32 aes_cbc_std(UINT8 enc,UINT8 kid, UINT8 *src, UINT8 *dest);
BOOL version_update(UINT32 new_version, enum VER_TYPE flag);
INT32 aes_cbc_crypt(UINT8 *out, UINT32 *rlen, struct CRYPT_DATA_INFO *crypt_data, struct UKEY_INFO *ukey, UINT32 mode);
/*add for three ladder*/
RET_CODE decrypt_universal_key_ext(UINT8 *plain_key, UINT32 key_id);
RET_CODE encrypt_universal_key_ext(UINT32 key_id);
RET_CODE aes_cbc_decrypt_chunk_ext(UINT8*key , UINT32 blockid, UINT8 *out, UINT32 *len);
RET_CODE aes_cbc_decrypt_flash_data_ext (UINT8 *out,UINT8* plain_key, UINT32 block_addr, UINT32 block_len);
RET_CODE aes_decrypt_data_from_sram_ext(UINT8 *output,UINT8* inputkey, UINT8* inputdate,UINT32 block_len);
#endif /*_FLASH_CIPHER_H_*/
